package cn.stazxr.pool;

import cn.stazxr.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author talentestors
 * @version 1.0
 * @since 2025.05.21
 **/
@SuppressWarnings("ALL")
public class UserDao extends BaseDao {

	private static final Logger log = LoggerFactory.getLogger(UserDao.class);

	public static void main(String[] args) {
		User user = new User("stazxr", "126");
		UserDao userDao = new UserDao();
		List<User> users = userDao.selectAllUser();
		System.out.println(users);
		User userById = userDao.findUserById(1);
		System.out.println(userById);
	}

	// 保存用户
	public int saveUser(User user) {
		Connection connection = null;
		PreparedStatement statement = null;
		String sql = "insert into user values(?,?,?,?)";
		try {
			connection = DATASOURCE.getConnection();
			statement = connection.prepareStatement(sql);
			statement.setInt(1, user.getId());
			statement.setString(2, user.getUsername());
			statement.setString(3, user.getPassword());
			statement.setDate(4, (Date) user.getBirthday());
			int rows = statement.executeUpdate();
			return rows;
		} catch (SQLException e) {
			log.error(e.getMessage());
		} finally {
			closeAll(connection, statement, null);
		}
		return -1;
	}

	// 更新用户
	public int updateUser(User user) {
		Connection connection = null;
		PreparedStatement statement = null;
		User findUser = findUserById(user.getId());
		if (findUser == null) {
			log.error("用户不存在");
			return -1;
		} else {
			if (user.getPassword() != null && !findUser.getPassword().equals(user.getPassword())) {
				user.setPassword(findUser.getPassword());
			}
			if (user.getUsername() != null && !findUser.getUsername().equals(user.getUsername())) {
				user.setUsername(findUser.getUsername());
			}
			if (user.getBirthday() != null && !findUser.getBirthday().equals(user.getBirthday())) {
				user.setBirthday(findUser.getBirthday());
			}
		}
		String sql = "update user set username=?, password=?, birthday=? where id=?";
		try {
			connection = DATASOURCE.getConnection();
			statement = connection.prepareStatement(sql);
			statement.setString(1, user.getUsername());
			statement.setString(2, user.getPassword());
			statement.setDate(3, (Date) user.getBirthday());
			statement.setInt(4, user.getId());
			int rows = statement.executeUpdate();
			return rows;
		} catch (SQLException e) {
			log.error(e.getMessage());
		} finally {
			closeAll(connection, statement, null);
		}
		return -1;
	}

	public List<User> selectAllUser() {
		Connection connection = null;
		PreparedStatement statement = null;
		ResultSet resultSet = null;
		String sql = "select id, username, password, birthday from user";
		List<User> users = new ArrayList<>();
		try {
			connection = DATASOURCE.getConnection();
			statement = connection.prepareStatement(sql);
			resultSet = statement.executeQuery();
			while (resultSet.next()) {
				User user = new User();
				user.setId(resultSet.getInt("id"));
				user.setUsername(resultSet.getString("username"));
				user.setPassword(resultSet.getString("password"));
				user.setBirthday(resultSet.getDate("birthday"));
				users.add(user);
			}
		} catch (SQLException e) {
			log.error(e.getMessage());
		} finally {
			closeAll(connection, statement, resultSet);
		}
		return users;
	}

	// 查询用户
	public User findUserById(int id) {
		Connection connection = null;
		PreparedStatement statement = null;
		ResultSet resultSet = null;
		String sql = "select id, username, password, birthday from user where id=?";
		User user = null;
		try {
			connection = DATASOURCE.getConnection();
			statement = connection.prepareStatement(sql);
			statement.setInt(1, id);
			resultSet = statement.executeQuery();
			if (resultSet.next()) {
				user = new User();
				user.setId(resultSet.getInt("id"));
				user.setUsername(resultSet.getString("username"));
				user.setPassword(resultSet.getString("password"));
				user.setBirthday(resultSet.getDate("birthday"));
			}
		} catch (SQLException e) {
			log.error(e.getMessage());
		} finally {
			closeAll(connection, statement, resultSet);
		}
		return user;
	}

	// 删除用户
	public int updateUser(int id) {
		Connection connection = null;
		PreparedStatement statement = null;
		String sql = "delete from user where id=?";
		try {
			connection = DATASOURCE.getConnection();
			statement = connection.prepareStatement(sql);
			statement.setInt(1, id);
			int rows = statement.executeUpdate();
			return rows;
		} catch (SQLException e) {
			log.error(e.getMessage());
		} finally {
			closeAll(connection, statement, null);
		}
		return -1;
	}

}
